//
//  HTTabBarController.m
//  HappyTravel
//
//  Created by Apple on 2018/8/2.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "HTTabBarController.h"

@interface HTTabBarController ()

/**
 *  设置之前选中的按钮
 */
@property (nonatomic, weak) UIButton *selectedBtn;

@end

@implementation HTTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)configPCUI {
    
    NSArray *normalArray = [NSArray array];
    NSArray *selectedArray = [NSArray array];
    
    normalArray = @[@"home",@"mood",@"tongji"];
    selectedArray = @[@"home_select",@"mood_select",@"tongji_select"];
    
    for (int i = 0; i < 3; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        NSString *imageName = normalArray[i];
        NSString *imageNameSel = selectedArray[i];
        
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];
        
        CGFloat x = i * self.tabBar.bounds.size.width / 3;
        btn.frame = CGRectMake(x, 0, self.tabBar.bounds.size.width / 3, self.tabBar.bounds.size.height);
        [self.tabBar addSubview:btn];
        
        btn.tag = i+100;
        [btn addTarget:self action:@selector(clickPCBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.selected = YES;
            self.selectedBtn = btn;
        }
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
}

- (void)resetTabBarView {
    for (UIView *view in self.tabBar.subviews) {
        [view removeFromSuperview];
    }
    
    [self configPCUI];
}


- (void)clickPCBtn:(UIButton *)button {
    NSInteger tag = button.tag - 100;
    
    if (button.tag == self.selectedBtn.tag) {
        return;
    }
    
    if (tag == 0 || tag == 1 || tag == 2) {
        
        self.selectedBtn.selected = NO;
        button.selected = YES;
        
        self.selectedBtn = button;
        
        if (tag == 2) {
            self.selectedIndex = 2;
        }else if (tag == 1){
            self.selectedIndex = 1;
        }else {
            self.selectedIndex = tag;
        }
    }
}







@end
